package xyz.csmcool.imoss.service.impl;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.PutObjectResult;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import xyz.csmcool.imoss.config.UploadTypeEnum;
import xyz.csmcool.imoss.entity.OssConfig;
import xyz.csmcool.imoss.mapper.OssConfigMapper;
import xyz.csmcool.imoss.service.UploadFile;
import xyz.csmcool.imoss.utils.NameUtil;

import java.io.File;

/**
 * @author 什锦
 * @Package xyz.csmcool.imoss.service.impl
 * @since 2022/8/5 21:28
 */
@Component
@RequiredArgsConstructor
public class MinIoUpload implements UploadFile {

    final OssConfigMapper ossConfigMapper;

    @Override
    public UploadTypeEnum getUploadType() {
        return UploadTypeEnum.MINIO;
    }

    @Override
    public String upload(File file, String prefix) {

        OssConfig ossConfig = ossConfigMapper.selectConfig();

        AWSCredentials credentials = new BasicAWSCredentials(ossConfig.getSecretId(), ossConfig.getSecretKey());
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setSignerOverride("AWSS3V4SignerType");

        AmazonS3 s3Client = AmazonS3ClientBuilder
                .standard()
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(ossConfig.getRegion(), Regions.US_EAST_1.name()))
                .withPathStyleAccessEnabled(true)
                .withClientConfiguration(clientConfiguration)
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .build();


        try {
            // 上传文件
            String fileName = ossConfig.getPrefix() + prefix + NameUtil.generateUniqueName(file.getName());
            PutObjectResult putObjectResult = s3Client.putObject(new PutObjectRequest(ossConfig.getBucketName(),
                    fileName,
                    file));



            return ossConfig.getUrl() + "/" + ossConfig.getBucketName() + "/" + fileName;
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException, which " + "means your request made it "
                    + "to Amazon S3, but was rejected with an error response" + " for some reason.");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());

        } catch (AmazonClientException ace) {
            System.out.println("Caught an AmazonClientException, which " + "means the client encountered " + "an internal error while trying to "
                    + "communicate with S3, " + "such as not being able to access the network.");
            System.out.println("Error Message: " + ace.getMessage());
        }
        return null;
    }
}
